<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/2
 * Time: 9:53
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Counselor extends Base
{
    /**
     * 顾问列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $counselor_wx = trim(input('counselor_wx'), ' '); // 微信号
            $counselor_phone = trim(input('counselor_phone'), ' '); // 手机号
            
            $where['counselor_status'] = ['between', [1, 2]];
            if(isset($counselor_wx) && $counselor_wx != ''){
                $where['counselor_wx'] = ['like', '%' . $counselor_wx . '%'];
            }
            if(isset($counselor_phone) && $counselor_phone != ''){
                $where['counselor_phone'] = ['like', '%' . $counselor_phone . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('counselor')
                ->join('up_area a1', 'a1.district_id = counselor_sheng', 'LEFT')
                ->join('up_area a2', 'a2.district_id = counselor_shi', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('counselor')
                ->join('up_area a1', 'a1.district_id = counselor_sheng', 'LEFT')
                ->join('up_area a2', 'a2.district_id = counselor_shi', 'LEFT')
                ->field('counselor_id, counselor_work_year, counselor_wx, counselor_phone, 
                counselor_image, counselor_star, counselor_status, a1.district sheng, a2.district shi')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('counselor_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加顾问
     * @return mixed|\think\response\Json
     */
    public function counselorAdd(){
        if(Request::instance()->isPost()){
            $counselor_work_year = trim(input('counselor_work_year'));
            $counselor_wx = input('counselor_wx');
            $counselor_phone = trim(input('counselor_phone'));
            $counselor_image = input('counselor_image');
            $counselor_star = trim(input('counselor_star'));
            $counselor_sheng = input('counselor_sheng');
            $counselor_shi = input('counselor_shi');
            
            if(!preg_match("/^1[3456789]\d{9}$/", $counselor_phone)){
                return json(['status' => 0, 'msg' => '请填写正确的手机号']);
            }
    
            $have = Db::table('up_counselor')
                ->where(['counselor_status' => 1, 'counselor_shi' => $counselor_shi])
                ->find();
            if($have) return json(['code' => 100, 'msg' => '该地区已有顾问']);
            
            $insert = [
                'counselor_work_year' => $counselor_work_year,
                'counselor_wx' => $counselor_wx,
                'counselor_phone' => $counselor_phone,
                'counselor_image' => $counselor_image,
                'counselor_star' => $counselor_star,
                'counselor_sheng' => $counselor_sheng,
                'counselor_shi' => $counselor_shi,
                'counselor_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_counselor')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
            
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign('province', $province);
        return $this->fetch();
    }
    
    /**
     * 编辑顾问
     * @return mixed|\think\response\Json
     */
    public function counselorEdit(){
        $counselor_id = input('counselor_id');
        if(Request::instance()->isPost()){
            $counselor_work_year = trim(input('counselor_work_year'));
            $counselor_wx = input('counselor_wx');
            $counselor_phone = trim(input('counselor_phone'));
            $counselor_image = input('counselor_image');
            $counselor_star = trim(input('counselor_star'));
            $counselor_sheng = input('counselor_sheng');
            $counselor_shi = input('counselor_shi');
    
            if(!preg_match("/^1[3456789]\d{9}$/", $counselor_phone)){
                return json(['status' => 0, 'msg' => '请填写正确的手机号']);
            }
            
            $have = Db::table('up_counselor')
                ->where(['counselor_status' => 1, 'counselor_shi' => $counselor_shi, 'counselor_id' => ['<>', $counselor_id]])
                ->find();
            if($have) return json(['code' => 100, 'msg' => '该地区已有顾问']);
            
            $update = [
                'counselor_id' => $counselor_id,
                'counselor_work_year' => $counselor_work_year,
                'counselor_wx' => $counselor_wx,
                'counselor_phone' => $counselor_phone,
                'counselor_image' => $counselor_image,
                'counselor_star' => $counselor_star,
                'counselor_sheng' => $counselor_sheng,
                'counselor_shi' => $counselor_shi,
                'counselor_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_counselor')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $counselor = Db::table('up_counselor')->where('counselor_id', $counselor_id)
            ->field('counselor_work_year, counselor_wx, counselor_phone, counselor_image, counselor_star, 
            counselor_sheng, counselor_shi')
            ->find();
        $province = db('area')->where('level', 1)->field('district_id, district')->select();
        $city = db('area')->where('pid', $counselor['counselor_sheng'])->field('district_id, district')->select();
        $this->assign([
            'counselor_id' => $counselor_id,
            'counselor' => $counselor,
            'province' => $province,
            'city' => $city
        ]);
        return $this->fetch();
    }
    
    /**
     * 禁用
     * @return \think\response\Json
     */
    public function counselorDown(){
        $counselor_id = input('id');
        $res = Db::table('up_counselor')->where('counselor_id', $counselor_id)->setField('counselor_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function counselorUp(){
        $counselor_id = input('id');
        $counselor = Db::table('up_counselor')->where('counselor_id', $counselor_id)->field('counselor_shi')->find();
        $have = Db::table('up_counselor')
            ->where(['counselor_shi' => $counselor['counselor_shi'], 'counselor_status' => 1])
            ->find();
        if($have) return json(['code' => 100, 'msg' => '该区域已有顾问，请先删除或禁用']);
        $res = Db::table('up_counselor')->where('counselor_id', $counselor_id)->setField('counselor_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
        
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function counselorDel(){
        $counselor_id = input('id');
        $res = Db::table('up_counselor')->where('counselor_id', $counselor_id)->setField('counselor_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}